/**
 *
 * @param success 数据库连接成功的回调
 * @param err 数据库连接失败的回调
 */
const config = require("../config/config");
module.exports = function (success, err) {
    // 判断 是否传入第二个参数  为其封装默认值
    if(typeof err != "function") {
        err = function () {
            console.log("数据库连接失败");
        }
    }
    // 导入 mongoose
    const mongoose = require("mongoose");
    // 导入配置人机
    const {DBHOST, DBPORT, DBNAME} = require("../config/config");

    // 连接 mongodb 服务
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`);

    mongoose.connection.once("open", () => {
        success()
    });

    mongoose.connection.on("error", () => {
        err()
    });
}